package com.ffcs.cn.action.desktopcontrol.smComboBoxDatasource;

import com.supermap.data.EngineType;
import com.supermap.desktop.controls.ui.controls.SmDialog;
import com.supermap.desktop.controls.ui.controls.comboBox.SmComboBoxDatasource;
import com.supermap.desktop.core.ui.controls.GridBagConstraintsHelper;
import com.supermap.desktop.core.utilties.DefaultValues;
import com.supermap.desktop.core.utilties.EngineTypeUtilities;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.util.Arrays;
import java.util.List;


public class DialogSmComboBoxDatasource extends SmDialog {
	private SmComboBoxDatasource smComboBoxDatasource;

	private JCheckBox checkBoxSupportCreate;

	private JCheckBox checkBoxIncludeReadOnly;

	private JCheckBox checkBoxSupportOpen;

	private JLabel labelSupportEngineTypes;
	private JList<String> listSupportEngineTypes;

	public DialogSmComboBoxDatasource() {
		setTitle("SmComboBoxDatasource 示例");
		setSize(390,360);
		initComponents();
		initLayout();
		initListener();
	}

	private void initListener() {
		//监听设置支持打开
		checkBoxSupportOpen.addActionListener(e -> smComboBoxDatasource.setSupportOpen(checkBoxSupportOpen.isSelected()));
		//监听设置支持新建
		checkBoxSupportCreate.addActionListener(e -> smComboBoxDatasource.setSupportCreate(checkBoxSupportCreate.isSelected()));
		//监听设置支持只读
		checkBoxIncludeReadOnly.addActionListener(e -> smComboBoxDatasource.setIncludeReadOnly(checkBoxIncludeReadOnly.isSelected()));
		//监听设置支持引擎类型
		listSupportEngineTypes.addListSelectionListener(e -> {
			List<String> selectedValuesList = listSupportEngineTypes.getSelectedValuesList();
			if (selectedValuesList != null && !selectedValuesList.isEmpty()){
				EngineType[] engineTypes = selectedValuesList.stream().map(EngineTypeUtilities::fromNetString).toArray(EngineType[]::new);
				smComboBoxDatasource.setSupportEngineTypes(engineTypes);
			}
		});
	}

	private void initLayout() {
		this.setLayout(new GridBagLayout());
		this.add(getLeftPanel(), new GridBagConstraintsHelper(0, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP)
				.setWeight(1, 1)
				.setFill(GridBagConstraints.BOTH));

		this.add(getRightPanel(), new GridBagConstraintsHelper(1, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0,
						GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setWeight(1, 1)
				.setFill(GridBagConstraints.BOTH));
	}

	private Component getRightPanel() {
		JPanel right = new JPanel();
		right.setLayout(new GridBagLayout());
		right.setBorder(new TitledBorder("控制面板"));
		right.add(checkBoxSupportCreate, new GridBagConstraintsHelper(0, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST).setWeight(1,1).setFill(GridBagConstraints.HORIZONTAL));

		right.add(checkBoxIncludeReadOnly, new GridBagConstraintsHelper(0, 1, 1, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST).setWeight(1,1).setFill(GridBagConstraints.HORIZONTAL));

		right.add(checkBoxSupportOpen, new GridBagConstraintsHelper(0, 2, 1, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST).setWeight(1,1).setFill(GridBagConstraints.HORIZONTAL));

		right.add(labelSupportEngineTypes, new GridBagConstraintsHelper(0, 3, 1, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST).setWeight(1,1).setFill(GridBagConstraints.HORIZONTAL));

		JScrollPane scrollPane = new JScrollPane(listSupportEngineTypes);
		scrollPane.setPreferredSize(new Dimension(100,100));
		right.add(scrollPane, new GridBagConstraintsHelper(0, 4, 1, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST).setWeight(1,1).setFill(GridBagConstraints.BOTH));
		return right;
	}

	private Component getLeftPanel() {
		JPanel panel = new JPanel();
		panel.setLayout(new GridBagLayout());
		panel.setBorder(new TitledBorder("控件"));
		smComboBoxDatasource.setPreferredSize(new Dimension(100,DefaultValues.getDefaultComponentHeight()));
		panel.add(new JLabel("数据源:"), new GridBagConstraintsHelper(0, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, 0)
				.setAnchor(GridBagConstraints.WEST));
		panel.add(smComboBoxDatasource, new GridBagConstraintsHelper(1, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST)
				.setWeight(1, 1).setFill(GridBagConstraints.HORIZONTAL));
		return panel;
	}

	private void initComponents() {
		smComboBoxDatasource = new SmComboBoxDatasource();

		checkBoxSupportCreate = new JCheckBox("支持新建数据源");
		checkBoxSupportCreate.setSelected(smComboBoxDatasource.isSupportCreate());

		checkBoxIncludeReadOnly = new JCheckBox("支持只读数据源");
		checkBoxIncludeReadOnly.setSelected(smComboBoxDatasource.isIncludeReadOnly());

		checkBoxSupportOpen = new JCheckBox("支持打开数据源");
		checkBoxSupportOpen.setSelected(smComboBoxDatasource.isSupportOpen());

		labelSupportEngineTypes = new JLabel("支持引擎类型：");
		listSupportEngineTypes = new JList<>();
		EngineType[] types = EngineTypeUtilities.getAllDatasourceDatabaseEngineTypes();
		String[] initTypes = Arrays.stream(types).map(EngineTypeUtilities::getEngineName).toArray(String[]::new);
		listSupportEngineTypes.setListData(initTypes);
	}


}
